import api from './index'

export const userManagementAPI = {
  // 用户管理
  getUsers(params) {
    return api.get('/user-management/users/', { params })
  },

  getUser(id) {
    return api.get(`/user-management/users/${id}/`)
  },

  createUser(data) {
    return api.post('/user-management/users/', data)
  },

  updateUser(id, data) {
    return api.patch(`/user-management/users/${id}/`, data)
  },

  deleteUser(id) {
    return api.delete(`/user-management/users/${id}/`)
  },

  resetUserPassword(id, data) {
    return api.post(`/user-management/users/${id}/reset_password/`, data)
  },

  toggleUserStatus(id) {
    return api.post(`/user-management/users/${id}/toggle_status/`)
  },

  // 导入导出功能
  exportTemplate() {
    return api.get('/user-management/users/export_template/', {
      responseType: 'blob'
    })
  },

  importUsers(file) {
    const formData = new FormData()
    formData.append('file', file)
    return api.post('/user-management/users/import_users/', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  exportUsers(params = {}) {
    return api.post('/user-management/users/export_users/', params, {
      responseType: 'blob'
    })
  },

  // 用户组管理
  getUserGroups(params) {
    return api.get('/user-management/user-groups/', { params })
  },

  getUserGroup(id) {
    return api.get(`/user-management/user-groups/${id}/`)
  },

  createUserGroup(data) {
    return api.post('/user-management/user-groups/', data)
  },

  updateUserGroup(id, data) {
    return api.put(`/user-management/user-groups/${id}/`, data)
  },

  deleteUserGroup(id) {
    return api.delete(`/user-management/user-groups/${id}/`)
  },

  getUserGroupUsers(id) {
    return api.get(`/user-management/user-groups/${id}/users/`)
  },

  getUserGroupRoles(id) {
    return api.get(`/user-management/user-groups/${id}/roles/`)
  },

  assignRolesToGroup(id, data) {
    return api.post(`/user-management/user-groups/${id}/assign_roles/`, data)
  },

  addUsersToGroup(id, data) {
    return api.post(`/user-management/user-groups/${id}/add_users/`, data)
  },

  removeUsersFromGroup(id, data) {
    return api.post(`/user-management/user-groups/${id}/remove_users/`, data)
  },

  // 角色菜单管理
  getRoleMenus(id) {
    return api.get(`/user-management/roles/${id}/menus/`)
  },

  assignMenusToRole(id, data) {
    return api.post(`/user-management/roles/${id}/assign_menus/`, data)
  },

  // 用户菜单管理
  getUserMenusDetail(userId) {
    return api.get(`/user-management/users/${userId}/menus_detail/`)
  },

  // 角色管理
  getRoles(params) {
    return api.get('/user-management/roles/', { params })
  },

  getRole(id) {
    return api.get(`/user-management/roles/${id}/`)
  },

  createRole(data) {
    return api.post('/user-management/roles/', data)
  },

  updateRole(id, data) {
    return api.put(`/user-management/roles/${id}/`, data)
  },

  deleteRole(id) {
    return api.delete(`/user-management/roles/${id}/`)
  },

  getRolePermissions(id) {
    return api.get(`/user-management/roles/${id}/permissions/`)
  },

  assignRolePermissions(id, data) {
    return api.post(`/user-management/roles/${id}/assign_permissions/`, data)
  },

  // 新增的角色管理API
  getRoleUsers(id) {
    return api.get(`/user-management/roles/${id}/users/`)
  },

  getRolePermissionsDetail(id) {
    return api.get(`/user-management/roles/${id}/permissions_detail/`)
  },

  checkRoleDeletionSafety(id) {
    return api.get(`/user-management/roles/${id}/deletion_safety_check/`)
  },

  cloneRole(id, data) {
    return api.post(`/user-management/roles/${id}/clone/`, data)
  },

  batchDeleteRoles(roleIds) {
    return api.post('/user-management/roles/batch_delete/', { role_ids: roleIds })
  },

  batchAssignPermissions(data) {
    return api.post('/user-management/roles/batch_assign_permissions/', data)
  },



  // 权限管理
  getPermissions(params) {
    return api.get('/user-management/permissions/', { params })
  },

  getPermission(id) {
    return api.get(`/user-management/permissions/${id}/`)
  },

  createPermission(data) {
    return api.post('/user-management/permissions/', data)
  },

  updatePermission(id, data) {
    return api.put(`/user-management/permissions/${id}/`, data)
  },

  deletePermission(id) {
    return api.delete(`/user-management/permissions/${id}/`)
  },

  // 菜单管理
  getMenus(params) {
    return api.get('/user-management/menus/', { params })
  },

  getMenuTree() {
    return api.get('/user-management/menus/tree/')
  },

  getAllMenus() {
    return api.get('/user-management/menus/all_menus/')
  },

  updateMenuOrder(data) {
    return api.post('/user-management/menus/update_order/', data)
  },

  getMenu(id) {
    return api.get(`/user-management/menus/${id}/`)
  },

  createMenu(data) {
    return api.post('/user-management/menus/', data)
  },

  updateMenu(id, data) {
    return api.put(`/user-management/menus/${id}/`, data)
  },

  deleteMenu(id) {
    return api.delete(`/user-management/menus/${id}/`)
  },

  // 菜单权限管理
  getRoleMenuMatrix() {
    return api.get('/user-management/menu-permissions/role_menu_matrix/')
  },

  batchAssignMenuPermissions(data) {
    return api.post('/user-management/menu-permissions/batch_assign/', data)
  },

  getMenuPermissions(id) {
    return api.get(`/user-management/menus/${id}/permissions/`)
  },

  // 菜单权限管理
  getMenuPermissionsList(params) {
    return api.get('/user-management/menu-permissions/', { params })
  },

  createMenuPermission(data) {
    return api.post('/user-management/menu-permissions/', data)
  },

  updateMenuPermission(id, data) {
    return api.put(`/user-management/menu-permissions/${id}/`, data)
  },

  deleteMenuPermission(id) {
    return api.delete(`/user-management/menu-permissions/${id}/`)
  },

  // 登录历史
  getLoginHistory(params) {
    return api.get('/user-management/login-history/', { params })
  }
}
